using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using BlueGameComponent;

namespace TouchExplode
{
    class CreditsScreen : MenuScreen
    {
        float OffsetY = 0;
        Texture2D background;
        const int MaxDistance = 860;

        public CreditsScreen()
            : base("Credits")
        {
            //IsPopup = true;
            EnabledGestures = GestureType.Tap | GestureType.FreeDrag;
            OffsetY = 0;
            Game1.AdMgr.SetVisible(false);
        }
        public override void LoadContent()
        {
            base.LoadContent();
            background = ScreenManager.Game.Content.Load<Texture2D>("Textures/backgroud");
        }
        public override void HandleInput(InputState input)
        {
            base.HandleInput(input);
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.ExitScreen();
            }

            if (!IsActive)
            {
                return;
            }

            // Return to the main menu when a tap gesture is recognized
            if (input.Gestures.Count > 0)
            {
                GestureSample sample = input.Gestures[0];
                if (sample.GestureType == GestureType.FreeDrag)
                {
                    if( sample.Delta.Y<0 )
                    {
                        Scroll(-sample.Delta.Y*2);
                    }
                }
                else if (sample.GestureType == GestureType.Tap)
                {
                    this.ExitScreen();

                    input.Gestures.Clear();
                }
            }
        }
        void Scroll(float f)
        { 
            OffsetY+=f;
            if (OffsetY>MaxDistance)
            {
                OffsetY = MaxDistance;
            }
        }
        public override void Draw(GameTime gameTime)
        {
            
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            spriteBatch.Begin();
            spriteBatch.Draw(background, new Vector2(0, 0), Color.White);
            //DrawTransition(background, new Vector2(0, 0), Color.White);
            spriteBatch.End();

            base.Draw(gameTime);

            spriteBatch.Begin();
            if (IsActive)
            {
                Vector2 Pos = new Vector2();
                int index = 0;
                foreach (string str in GameConstant.CreditsStr)
                {
                    Vector2 W_H = ScreenManager.Font.MeasureString(str);
                    Pos.X = ScreenManager.ViewPort.Width/2-W_H.X/2;
                    Pos.Y = ScreenManager.ViewPort.Height + index * (W_H.Y+10) - OffsetY;
                    spriteBatch.DrawString(ScreenManager.Font, str, Pos + new Vector2(2,2), Color.Gray);
                    spriteBatch.DrawString(ScreenManager.Font, str, Pos, Color.Black);
                    index++;
                }
            }
            spriteBatch.End();
            
        }
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
            if (IsActive)
            {
                Scroll(1.0f);
            }
        }
    }
}
